<?php
	class EventAttendeesController extends AppController{
		var $name = 'EventAttendees';
		var $uses = array('EventAttendee','Event');
    	
		function beforeFilter(){ 
	        $this->__validateLoginStatus();
	    }
	
	    function __validateLoginStatus(){ 
	       if($this->Session->check('User') == false){
	          $this->Session->setFlash('you must be logged in to access this page');
	          $this->redirect(array('controller' => 'users', 'action' => 'login'));
	       }
	    }    	
    	
    	function view($event_id = null){
    		if(!$event_id){
    			$this->Session->setFlash(__('Invalid event id',true));
    			$this->redirect(array('controller'=>HOME_CONTROLLER,'action'=>'index'));
    		}
    		$event = $this->Event->find('first',array('conditions'=>array('Event.id'=>$event_id),'recursive' => -1));
    		if(!$event){
    			$this->Session->setFlash(__('Invalid event id',true));
    			$this->redirect(array('controller'=>HOME_CONTROLLER,'action'=>'index'));
    		}
    		$accessArray = $this->Session->read('accessArray');
    		if ($this->Session->read('User.id') != $event['Event']['attendance_manager_id'] && !$accessArray['CreateEvent']) {
    			$this->Session->setFlash(__('You don\'t have the permission to access this page.',true));
    			$this->redirect(array('controller'=> 'events' ,'action'=>'main', $event_id));
    		}
    		$temp = $this->EventAttendee->find('all',array(
    										'conditions' => array(
    											'EventAttendee.event_id'=>$event_id
    										),
    										'fields' => array(
    											'EventAttendee.id',
    											'EventAttendee.user_id',
    											'EventAttendee.status',
    											'User.username',
    											'User.first_name',
    											'User.middle_name',
    											'User.last_name'    											    											
    										)
    									)
    								);
    		$event_tmp = array('title' => $event['Event']['title'], 'id' => $event['Event']['id']);
    		//debug($temp);
    		$this->set('eventAttendees',$temp);
    		$this->set('event', $event_tmp);
    	}
    	
    	//changes the user attend event status for the specified event
        function attendEvent($id=null,$status=null){
       		if(!$id){
    			$this->Session->setFlash(__('Invalid event id',true));
    			$this->redirect(array('controller'=>HOME_CONTROLLER,'action'=>'index'));
    		}
            $this->EventAttendee->read(null,$id);
            $this->EventAttendee->set('status', $status);
            $this->EventAttendee->save();
            if ($status == 1)
                echo 'confirmed';
            else
                echo 'pending';                
            exit();
        }
	}
?>